﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class out_daily : System.Web.UI.Page
{
    DBClass _db = new DBClass();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Load_Department();
            LoadData();
        }
    }
    protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        Load_Section();
    }
    private void Load_Department()
    {
        ddlDepartment.DataSource = _db.GetList_Department();
        ddlDepartment.DataBind();
        ListItem item = new ListItem("----------select---------", "");
        ddlDepartment.Items.Insert(0, item);
    }
    private void Load_Section()
    {
        string item = ddlDepartment.SelectedValue.Replace(" ", "");
        DataTable dt = _db.GetList_Section(item);

        ddlSection.DataSource = dt;
        ddlSection.DataBind();
        ListItem item2 = new ListItem("----------select---------", "");
        ddlSection.Items.Insert(0, item2);
    }
    protected void grvData_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    private void LoadData()
    {
        DataTable dt = new DataTable();
        DateTime? fromdate = null;
        DateTime? todate = null;
        if (txtAttnDateFrom.Text != "" || !String.IsNullOrEmpty(txtAttnDateFrom.Text))
        {
            fromdate = fromDate(txtAttnDateFrom.Text + " 00:00:00");
        }
        if (txtAttnDateTo.Text != "" || !String.IsNullOrEmpty(txtAttnDateTo.Text))
        {
            todate = toDate(txtAttnDateTo.Text + " 23:59:59");
        }
        string dept = null;
        if (ddlDepartment.SelectedIndex > 0)
            dept = ddlDepartment.SelectedValue.Replace(" ", "");
        string section = null;
        if (ddlSection.SelectedIndex > 0)
            section = ddlSection.SelectedValue.Replace(" ", "");
        string empcode = null;
        if (txtEmpCode.Text != "" || !String.IsNullOrEmpty(txtEmpCode.Text))
            empcode = txtEmpCode.Text;

        dt = _db.Select_EmpDaily("mint", fromdate, todate, dept, section, empcode, null, null, null, null, null, null, null);

        _PageDataSource.DataSource = dt.DefaultView;
        _PageDataSource.AllowPaging = true;
        _PageDataSource.PageSize = ToSQL.SQLToInt(txtPageNumber.Text);
        _PageDataSource.CurrentPageIndex = CurrentPage;
        ViewState["TotalPages"] = _PageDataSource.PageCount;

        grvData.DataSource = _PageDataSource;
        grvData.DataBind();

        lbTotal.Text = "Total: " + dt.Rows.Count.ToString();
        this.doPaging();
        grvData.UseAccessibleHeader = true;
        grvData.HeaderRow.TableSection = TableRowSection.TableHeader; 
    }
    protected void grvData_PageIndexChanged(object sender, EventArgs e)
    {

    }
    protected void grvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvData.PageIndex = e.NewPageIndex;
        LoadData();
    }
    protected void btnFilter_Click(object sender, EventArgs e)
    {
        LoadData();
    }
    /**
        * [Get From Date] get from date.
        * */
    private DateTime fromDate(string fromTime)
    {
        DateTime fromT = DateTime.MinValue;
        DateTime.TryParse(fromTime, out fromT);
        if (fromT == DateTime.MinValue)
            fromT = DateTime.Parse("1/1/1753 12:00:00 AM");
        return fromT;
    }
    /**
    * [Get TO Date] get to date.
    * */
    private DateTime toDate(string toTime)
    {
        DateTime toT;
        DateTime.TryParse(toTime, out toT);
        if (toT == DateTime.MinValue)
            toT = DateTime.MaxValue;
        return toT;
    }
    /**
       * [Filter] filter data with datetime selected.
       * */
    protected void grvData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
    }
    #region Private Properties
        private int CurrentPage
        {
            get
            {
                object objPage = ViewState["_CurrentPage"];
                int _CurrentPage = 0;
                if (objPage == null)
                {
                    _CurrentPage = 0;
                }
                else
                {
                    _CurrentPage = (int)objPage;
                }
                return _CurrentPage;
            }
            set { ViewState["_CurrentPage"] = value; }
        }
        private int fistIndex
        {
            get
            {

                int _FirstIndex = 0;
                if (ViewState["_FirstIndex"] == null)
                {
                    _FirstIndex = 0;
                }
                else
                {
                    _FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
                }
                return _FirstIndex;
            }
            set { ViewState["_FirstIndex"] = value; }
        }
        private int lastIndex
        {
            get
            {

                int _LastIndex = 0;
                if (ViewState["_LastIndex"] == null)
                {
                    _LastIndex = 0;
                }
                else
                {
                    _LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
                }
                return _LastIndex;
            }
            set { ViewState["_LastIndex"] = value; }
        }
        #endregion

        #region PagedDataSource
        PagedDataSource _PageDataSource = new PagedDataSource();
        #endregion

        
        private void doPaging()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("PageIndex");
            dt.Columns.Add("PageText");
            fistIndex = CurrentPage - 5;
            if (CurrentPage > 5)
            {
                lastIndex = CurrentPage + 5;
            }
            else
            {
                lastIndex = 10;
            }
            if (lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
            {
                lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
                fistIndex = lastIndex - 10;
            }
            if (fistIndex < 0)
            {
                fistIndex = 0;
            }
            for (int i = fistIndex; i < lastIndex; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = i;
                dr[1] = i + 1;
                dt.Rows.Add(dr);
            }
            this.dlPaging.DataSource = dt;
            this.dlPaging.DataBind();
        }
        protected void btnNext_Click(object sender, EventArgs e)
        {
        }

        
        protected void lbtnLast_Click(object sender, EventArgs e)
        {
            CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
            this.LoadData();
        }
        protected void lbtnFirst_Click(object sender, EventArgs e)
        {
            CurrentPage = 0;
            this.LoadData();
        }
        protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName.Equals("Paging"))
            {
                CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
                this.LoadData();
            }
        }
        protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
            if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
            {
                lnkbtnPage.Enabled = false;
                lnkbtnPage.Style.Add("fone-size", "14px");
                lnkbtnPage.Font.Bold = true;
                lnkbtnPage.ForeColor = System.Drawing.ColorTranslator.FromHtml("#000");
            }
        }    

        protected void txtPageNumber_TextChanged(object sender, EventArgs e)
        {
            LoadData();
        }
        protected void Image1_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage += 1;
            this.LoadData();
        }
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
             CurrentPage -= 1;
            this.LoadData();
        }
}